const scrollableView = () => {
  let scrollEl = className("androidx.recyclerview.widget.RecyclerView").scrollable(true).findOnce();

}

const treasureBoxWin = () => {
  let i = 0;
  let advertiseReg: RegExp = /看广告视频.+(\d{3})/;
  // let mysteryAdvertiseReg: RegExp = /看广告视频再赚\d{3}/;
  let img = screenshot()
  let res = paddle.ocr(img)
  img.recycle()

  for (let index = 0; index < res.length; index++) {
    let item = res[index];
    if(!advertiseReg.test(item.text)) continue;
    let n = advertiseReg.exec(item.text)
    let num = n?.length ? Number(n[1]) : 0
    printLog(`广告宝箱：${num}`);
    if(num > 200){
      i++;
      let rect = item.bounds
      humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
      sleep(4000)
      let el = textContains('秒后可领奖励').findOnce()
      let el2 = textContains('秒后开启神秘宝箱').findOnce()
      if(el || el2){
        let str = el?.text() || el2?.text();
        let arr = str?.match(/(\d+).+/)
        let num = arr?.length && arr[1] 
        console.log('等待时间：',num);
        printLog(`等待时间：${num}`)
        if(num){
          let s = (parseInt(num) + 5) * 1000
          sleep(s)
        }
        let rect = textContains("领取成功").findOne()?.bounds()
        if(rect){
          humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
          sleep(3000)
        }
      }
      break;
    }

   
  }


  let reg: RegExp = /(\d{3,}).+/
  let el = textMatches(reg).findOnce()
  if(el && textContains('再看一个视频').exists()){
    let n = reg.exec(el.text())
    let num = n?.length ? Number(n[1]) : 0
    printLog(`广告宝箱：${num}`);
    if(num > 200){
      i++;
      let rect = text('领取奖励').findOnce()?.bounds()
      humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
      sleep(3000)
      el = textContains('秒后可领奖励').findOnce()
      if(el){
        let str = el?.text()
        let arr = str?.match(/(\d+).+/)
        let num = arr?.length && arr[1] 
        console.log('等待时间：',num);
        printLog(`等待时间：${num}`)
        if(num){
          let s = (parseInt(num) + 5) * 1000
          sleep(s)
        }
        let rect = textContains("领取成功").findOne()?.bounds()
        if(rect){
          humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
          sleep(3000)
        }
      }
    };
  }

  if(!i)return i;
  let count: number = treasureBoxWin();
  return i + count;
}

// 开启宝箱操作
exports.treasureBoxLogic = () => {
  let state: { daily: any, treasureBox: any } = {
    daily: null,
    treasureBox: null
  }

  const ocr = () => {
    let img = screenshot()
    let res = paddle.ocr(img)
    img.recycle()
    console.log(JSON.stringify(res))
    let receiveReg: RegExp = /点击领\d{1,}/
    let dailyReg: RegExp = /立即签到.+/
  
    for (let index = 0; index < res.length; index++) {
      let item = res[index];
      if(dailyReg.test(item.text)){
        state.daily = item
        break;
      }
      if(item.text.includes('开宝箱得金币') || receiveReg.test(item.text) || item.text.includes('开启神秘宝箱')){
        state.treasureBox = item
        break;
      }
    }
  
  }
  
  sleep(10000)

  ocr()

  
  // 判断签到页面
  //-------start-----
  if(state.daily){
    console.log('签到页面');
    let rect = state.daily.bounds
    humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    sleep(2000)
    back();
    sleep(2000)
  }
    //-------end-------


  if(state.daily){
    state.daily = null
    ocr()
  }

  // 开启宝箱
  //-------start-----
  if(state.treasureBox){
    console.log('宝箱', state.treasureBox.text);
    let rect = state.treasureBox.bounds
    humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    sleep(2000)
    treasureBoxWin()
    // let img = screenshot()
    // let res = paddle.ocr(img)
    // img.recycle()
    // // /看广告视频打开\d{3}/
    // // 广告宝箱
    // let advertiseReg: RegExp = /看广告视频打开\d{3}/
    // let mysteryAdvertiseReg: RegExp = /看广告视频再赚\d{3}/
    // for (let index = 0; index < res.length; index++) {
    //   let item = res[index];
    //   console.log(advertiseReg.test(item.text) , mysteryAdvertiseReg.test(item.text));
      
    //   if(advertiseReg.test(item.text) || mysteryAdvertiseReg.test(item.text)){
    //     console.log("广告宝箱");
        
    //     let rect = item.bounds
    //     humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    //     sleep(4000)
    //     let el = textContains('秒后可领奖励').findOnce()
    //     let el2 = textContains('秒后开启神秘宝箱').findOnce()
    //     if(el || el2){
    //       let str = el?.text() || el2?.text();
    //       let arr = str?.match(/(\d+).+/)
    //       let num = arr?.length && arr[1] 
    //       console.log('等待时间：',num);
    //       printLog(`等待时间：${num}`)
    //       if(num){
    //         let s = (parseInt(num) + 5) * 1000
    //         sleep(s)
    //       }
    //       let rect = textContains("领取成功").findOne()?.bounds()
    //       if(rect){
    //         humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    //         sleep(2000)
    //         let reg: RegExp = /\d{3,}/
    //         let el = textMatches(reg).findOnce()
    //         if(el){
    //           let rect = text('领取奖励').findOnce()?.bounds()
    //           humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    //           sleep(2000)
    //           let el = textContains('秒后可领奖励').findOnce()
    //           if(el){
    //             let str = el?.text()
    //             let arr = str?.match(/(\d+).+/)
    //             let num = arr?.length && arr[1] 
    //             console.log('等待时间：',num);
    //             printLog(`等待时间：${num}`)
    //             if(num){
    //               let s = (parseInt(num) + 5) * 1000
    //               sleep(s)
    //             }
    //             let rect = textContains("领取成功").findOne()?.bounds()
    //             if(rect){
    //               humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    //             }
    //           }
    //         }
    //       }
    //       sleep(4000)
    //     }
    //     break
    //   }
     
    // }
  }
  printLog(`已开启宝箱`)
  let img = screenshot()
  let res = paddle.ocr(img)
  img.recycle()
  for(let i = 0; i < res.length; i++){
    let item = res[i]
    if(item.text.includes('开心收下')){
      printLog('开心收下');
      
      let rect = item.bounds
      humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
      break
    }
    if(item.text.includes('评价并收下金币')){
      printLog('评价并收下金币');
      let rect = item.bounds
      humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
      break
    }

    if(item.text.includes('坚持退出')){
      printLog('坚持退出');
      let rect = item.bounds
      humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
      break
    }
    // if(item.text.includes('直接打开')){
    //   printLog('直接打开');
    //   let rect = item.bounds
    //   humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    // }
  }
  sleep(2000);
  

  //-------end-------

  // console.log('----------------');
  // res.forEach(item => {
  //   console.log(item.text);
    
  // });
  // console.log('----------------');
  
  // 退出之前获取下次进入时间以及部分信息
  img = screenshot()
  res = paddle.ocr(img)
  console.log(JSON.stringify(res))
  res.forEach(item=>{
    let reg: RegExp = /(\d{2})分(\d{2})秒.+/
    let match:null|Array<string> = item.text.match(reg)
    if(match){
      state.treasureBox = parseInt(match[1]) * 60 * 1000 + parseInt(match[2]) * 1000
      printLog(`剩余开启时间 ${match[1]} 分 ${match[2]} 秒`)
    }
  })
  sleep(2000)
  printLog(`退出`)
  back();
  return state.treasureBox
}


exports.popupWindow = () => {
  if (text('关闭').exists()) {
    let rect = text('关闭').findOnce()?.bounds()
    humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    return;
  }
  if (desc('关闭').exists()) {
    let rect = desc('关闭').findOnce()?.bounds()
    humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    return;
  }
  if (text('知道了').exists()) {
    let rect = text('知道了').findOnce()?.bounds()
    humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    return;
  }
  if (text("发现通讯录朋友").exists()) {
    let rect = text("拒绝").findOnce()?.bounds()
    humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    return;
  }
  // 青少年模式
  if (text("青少年模式").exists()) {
    let rect = text("我知道了").findOnce()?.bounds()
    humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    return;
  }
  // 个人信息保护指引
  if (text("个人信息保护指引").exists()) {
    let rect = text("同意").findOnce()?.bounds()
    humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    return;
  }
  // 版本更新
  if (text("检测到更新").exists()) {
    let rect = text("以后再说").findOnce()?.bounds()
    humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    return;
  }
  // 内测版本更新
  if (text("新版本内测邀请").exists()) {
    let rect = text("以后再说").findOnce()?.bounds()
    humanLikeGesture(rect.left, rect.top, rect.right, rect.bottom)
    return;
  }
}



exports.taskList  = () => { 
  let list = [

  ]

}



